package com.bxw.forum.service.impl;

import com.bxw.forum.common.AppResult;
import com.bxw.forum.common.ResultCode;
import com.bxw.forum.dao.UserLikeMapper;
import com.bxw.forum.exception.ApplicationException;
import com.bxw.forum.model.UserLike;
import com.bxw.forum.service.IUserLikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Slf4j
@Service
public class UserLikeServiceImpl implements IUserLikeService {

    @Resource
    private UserLikeMapper userLikeMapper;
    @Override
    public UserLike selectById(Long userId, Long articleId) {
        if(userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        UserLike userLike = userLikeMapper.selectById(userId,articleId);

        return userLike;
    }

    @Override
    public void insert(Long userId, Long articleId) {
        if(userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        int row = userLikeMapper.insert(userId,articleId);

        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_SERVICES));
        }
    }
}
